package datastructure.selfexercise;

import java.lang.reflect.Array;
import java.util.*;

/**
 * @Author Gan
 * @Date 2021/5/12 15:36
 * @Description:
 * 示例 2：
 * 输入：nums = [3,30,34,5,9]
 * 输出："9534330"
 * 示例 4：
 * 输入：nums = [10]
 * 输出："10"
 */
public class Junit03 {
    public static void main(String[] args) {
        int[] nums = new int[]{0,0,0,0};
        System.out.println(largestNumber(nums));
    }
    private static String largestNumber(int[] nums) {
        if(nums.length == 0)
            return "0";
        if(nums.length == 1){
            return String.valueOf(nums[0]);
        }
        //将int数组转化为string数组
        String[] arr = new String[nums.length];
        for(int i = 0;i < arr.length;i++){
            arr[i] = String.valueOf(nums[i]);
        }
        Arrays.sort(arr, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //若是正值就交换，负值不交换
                return (o2 + o1).compareTo(o1 + o2);
            }
        });
        StringBuilder sb = new StringBuilder();
        for (String str:arr){
            sb.append(str);
        }
        String ans = sb.toString();
        if (ans.charAt(0) == '0')
            ans = "0";
        return ans;
    }
}
